#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
FlaSam Code

2007 2007-6-28 下午02:35:59
'''
from django.conf import settings
from django.conf.urls.defaults import *
from django.contrib.auth.models import User
import os
import os.path

def make_url_list(input_url, prefix=''):
    '''
    make_url_list([('widget', Widget)])

    will create: 

    ^/widget/$  # will list all widgets
    ^/widget/(?<object_id>\d+)/$ # will give details for a specific object
    ^/widget/edit/(?<object_id>\d+)/$ # will edit an object
    ^/widget/delete/(?<object_id>\d+)/$ # will delete an object
    ^/widget/add/$ # will add an object
OK

    @param input:
    @param prefix:
    
    example:

#===============================================================================
#    tup = tuple(make_url_list(input,prefix='/prefix_if_needed'))
# 
#    urlpatterns = patterns('',
#        (r'^someotherurl/$', 'some.other.package'),
#        *tup)
#===============================================================================

    '''
    _t = []

    for (name, model) in input_url:
        _t.append((r'^%s/$' % name, 
            'django.views.generic.list_detail.object_list', 
            dict(queryset=model.objects.all(), allow_empty=True)))

        _t.append((r'^%s/(?P<object_id>\d+)/$' %name, 
            'django.views.generic.list_detail.object_detail', 
            dict(queryset=model.objects.all())))

        _t.append((r'^%s/add/$' % name, 
            'django.views.generic.create_update.create_object', 
            dict(model=model, 
                post_save_redirect='%s/%s/%s/' % (prefix, name, '%(id)s'))))

        _t.append((r'^%s/edit/(?P<object_id>\d+)/$' % name, 
            'django.views.generic.create_update.update_object', 
            dict(model=model, 
                post_save_redirect='%s/%s/%s/' % (prefix, name, '%(id)s'))))

        _t.append((r'^%s/delete/(?P<object_id>\d+)/$' % name, 
            'django.views.generic.create_update.delete_object', 
            dict(model=model, 
                post_delete_redirect='%s/%s/' % (prefix, name))))
    return _t

user_dict = {
             'queryset': User.objects.all(), 
             }

urlpatterns = patterns('', 
    # Example:
    # (r'^server_side/', include('server_side.foo.urls')),
    
     # root view
     (r'^$', 'apps.ad_manager.ad_manager_views.index'), \
     
     # super admin view
     (r'^admin/', include('django.contrib.admin.urls')), \
     
     # user manager view
     (r'^users/$', 'apps.ad_usergroup.ad_usergroup_views.users_list'), \
     (r'^users/(?P<i>\d*)/$', 'apps.ad_usergroup.ad_usergroup_views.users_detail'), \
     (r'^users/(?P<i>\d*)/del/$', 'apps.ad_usergroup.ad_usergroup_views.users_delete'), \
     (r'^users/(?P<i>\d*)/chgstate/$', 'apps.ad_usergroup.ad_usergroup_views.users_change_state'), \
     (r'^users/(?P<i>\d*)/chgpass/$', 'apps.ad_usergroup.ad_usergroup_views.users_change_passwd'), \
     (r'^users/add/$', 'apps.ad_usergroup.ad_usergroup_views.users_create'), \
     
     # group manager view
     (r'^group/$', 'apps.ad_usergroup.ad_usergroup_views.group_list'), \
     (r'^group/(?P<i>\d*)/$', 'apps.ad_usergroup.ad_usergroup_views.group_detail'), \
     
     # user selfservices view
     (r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}), \
     (r'^accounts/logout/$', 'django.contrib.auth.views.logout_then_login', {'login_url':'/accounts/login'}), \
     # user update himself account info
     (r'^accounts/profile/$', 'apps.ad_usergroup.ad_usergroup_views.accounts_profile_deatil_user_himself'), \
     # admin update account info, param <i> is user id, not account tab id
     (r'^accounts/(?P<i>\d*)/$', 'apps.ad_usergroup.ad_usergroup_views.accounts_profile_detail_admin_update'), \
     # admin view account info, param <i> is user id, not account tab id
     (r'^accounts/(?P<i>\d*)/view/$', 'apps.ad_usergroup.ad_usergroup_views.accounts_profile_detail_admin_view'), \
     # user update himself login password
     (r'^accounts/chgpass/$', 'apps.ad_usergroup.ad_usergroup_views.accounts_change_passwd_himself'), \
     
     # place manager view
     (r'^place/$', 'apps.ad_usergroup.ad_usergroup_views.place_list'), \
     (r'^map/$', 'apps.ad_usergroup.ad_usergroup_views.place_map'), \
     (r'^place/(?P<i>\d*)/$', 'apps.ad_usergroup.ad_usergroup_views.place_detail'), \
     (r'^place/add/$', 'apps.ad_usergroup.ad_usergroup_views.place_create'), \
     (r'^place/(?P<i>\d*)/del/$', 'apps.ad_usergroup.ad_usergroup_views.place_delete'), \
     (r'^place/parent/(?P<i>\d*|None)/$', 'apps.ad_usergroup.ad_usergroup_views.place_parent_list'), \
     
     # error_permission view
     (r'^error_permission/$', 'apps.ad_manager.ad_manager_views.error_permission'), \
     
     # adorg list view
     (r'^adorg/$', 'apps.ad_manager.ad_manager_views.adorg_list'), \
     (r'^adorg/(?P<i>\d*)/$', 'apps.ad_manager.ad_manager_views.adorg_detail'), \
     (r'^adorg/(?P<i>\d*)/del/$', 'apps.ad_manager.ad_manager_views.adorg_delete'), \
     (r'^adorg/add/$', 'apps.ad_manager.ad_manager_views.adorg_create'), \
     
     # ad item list view
     (r'^aditem/$', 'apps.ad_manager.ad_manager_views.aditem_list'), \
     (r'^aditem/(?P<i>\d*)/$', 'apps.ad_manager.ad_manager_views.aditem_detail'), \
     (r'^aditem/(?P<i>\d*)/content/$', 'apps.ad_manager.ad_manager_views.aditem_content_detail'), \
     (r'^aditem/(?P<i>\d*)/chgstate/$', 'apps.ad_manager.ad_manager_views.aditem_change_state'), \
     (r'^aditem/add/$', 'apps.ad_manager.ad_manager_views.aditem_create'), \
     
     # news list
     (r'^news/$', 'apps.ad_news.ad_news_views.news_list'), \
     
     
 )

if settings.DEBUG:
    urlpatterns += patterns('', 
        (r'^medias/(?P<path>.*)$', 'django.views.static.serve', {'document_root': os.path.join(os.getcwd(), 'medias'), 'show_indexes': True}), 
    )


